package com.shandiangou.crm.controller;

import com.shandiangou.crm.model.vo.DataRightQueryVo;
import com.shandiangou.crm.model.vo.DataRightVo;
import com.shandiangou.crm.model.vo.OrderVo;
import com.shandiangou.crm.service.CrmService;
import com.shandiangou.crm.util.VoValidatorUtil;
import com.shandiangou.crm.web.HttpResult;
import com.shandiangou.member.domain.UserDO;
import com.shandiangou.member.service.UserService;
import com.shandiangou.member.session.SdgSessionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by jacky on 16/1/20.
 * CRM业务的Controllerc层
 */

@Controller
@RequestMapping("/crm")
public class CrmController {

    private static final Logger logger = LoggerFactory.getLogger(CrmController.class);

    @Autowired
    private UserService userService;//用户中心服务

    @Autowired
    private CrmService crmService;//crm服务

    /**
     * 获取用户信息
     *
     * @return
     */
    private UserDO getUser() {
        if (SdgSessionUtil.getUserMobile() != null) {
            String mobile = SdgSessionUtil.getUserMobile().toString();
            return userService.getUserByMobile(mobile);
        } else {
            return null;
        }
    }

    /**
     * 获得数据权限列表
     *
     * @param dataRightQueryVo
     * @return
     */
    @RequestMapping(value = "/listDataRightList", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult listDataRightList(@RequestBody DataRightQueryVo dataRightQueryVo) {
        try {
            UserDO oUserDO = getUser();
            if (oUserDO != null) {
                String validateStr = VoValidatorUtil.validateVo(dataRightQueryVo);
                if (oUserDO != null && (oUserDO.getLevel() >= 2000)) {//2000权限才可以操作
                    if (validateStr != null) {
                        return HttpResult.Failure(-9, validateStr);
                    }
                    return HttpResult.Success(crmService.listDataRightList(dataRightQueryVo));
                } else {
                    return HttpResult.Failure(-98, "权限不足！");
                }
            } else {
                return HttpResult.Failure(-98, "权限不足！");
            }
        } catch (Exception e) {
            logger.error("listDataRightList", e);
            return HttpResult.Failure(-99, "操作异常！");
        }
    }

    /**
     * 新增数据权限白名单
     *
     * @param dataRightVo
     * @return
     */
    @RequestMapping(value = "/addDataRight", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult addDataRight(@RequestBody DataRightVo dataRightVo) {
        try {
            UserDO oUserDO = getUser();
            if (oUserDO != null) {
                String validateStr = VoValidatorUtil.validateVo(dataRightVo);
                if (oUserDO != null && (oUserDO.getLevel() >= 2000)) {//2000权限才可以操作
                    if (validateStr != null) {
                        return HttpResult.Failure(-9, validateStr);
                    }
                    crmService.insertDataRight(dataRightVo);
                    return HttpResult.Success("新增数据权限白名单成功!");
                } else {
                    return HttpResult.Failure(-98, "权限不足！");
                }
            } else {
                return HttpResult.Failure(-98, "权限不足！");
            }
        } catch (Exception e) {
            logger.error("addDataRight", e);
            return HttpResult.Failure(-99, "操作异常！");
        }
    }

    /**
     * 修改数据权限白名单
     *
     * @param dataRightVo
     * @return
     */
    @RequestMapping(value = "/updateDataRight", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult updateDataRight(@RequestBody DataRightVo dataRightVo) {
        try {
            UserDO oUserDO = getUser();
            if (oUserDO != null) {
                String validateStr = VoValidatorUtil.validateVo(dataRightVo);
                if (oUserDO != null && (oUserDO.getLevel() >= 2000)) {//2000权限才可以操作
                    if (validateStr != null) {
                        return HttpResult.Failure(-9, validateStr);
                    }
                    crmService.updateDataRight(dataRightVo);
                    return HttpResult.Success("修改数据权限白名单成功!");
                } else {
                    return HttpResult.Failure(-98, "权限不足！");
                }
            } else {
                return HttpResult.Failure(-98, "权限不足！");
            }
        } catch (Exception e) {
            logger.error("updateDataRight", e);
            return HttpResult.Failure(-99, "操作异常！");
        }
    }

    /**
     * 删除数据权限白名单
     *
     * @param dataRightVo
     * @return
     */
    @RequestMapping(value = "/deleteDataRight", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult deleteDataRight(@RequestBody DataRightVo dataRightVo) {
        try {
            UserDO oUserDO = getUser();
            if (oUserDO != null) {
                if (oUserDO != null && (oUserDO.getLevel() >= 2000)) {//2000权限才可以操作
                    crmService.deleteDataRight(dataRightVo.getId());
                    return HttpResult.Success("删除数据权限白名单成功!");
                } else {
                    return HttpResult.Failure(-98, "权限不足！");
                }
            } else {
                return HttpResult.Failure(-98, "权限不足！");
            }
        } catch (Exception e) {
            logger.error("deleteDataRight", e);
            return HttpResult.Failure(-99, "操作异常！");
        }
    }
}
